Python translate() 方法 |
您所在的位置:网站首页 › python translate方法 › Python translate() 方法 |
Python 字符串对象的 translate() 方法根据参数 table 给出的表 ( 包含 256 个字符 )y用于转换字符串的字符, 要过滤掉的字符放到 deletechars 参数中 translate 一般和 string.maketrans 配合使用 语法 str.translate(table[, deletechars]); 参数 参数 说明 table 翻译表,翻译表是通过 str.maketrans 方法转换而来 deletechars 字符串中要过滤的字符列表 返回值返回翻译后的字符串 范例下面的代码使用 translate() 方法转换一些字符 >>> from string import maketrans # 引用 maketrans 函数 >>> intab = "aeiou" >>> outtab = "12345" >>> trantab = maketrans(intab, outtab) >>> s = "this is string example....wow" >>> print s.translate(trantab) th3s 3s str3ng 2x1mpl2....w4w 范例 2下面的代码使用 translate() 方法转换一些字符,并去除字符串中的 'x' 和 'm' 字符 >>> from string import maketrans # 引用 maketrans 函数 >>> intab = "aeiou" >>> outtab = "12345" >>> trantab = maketrans(intab, outtab) >>> s = "this is string example....wow" >>> print s.translate(trantab) th3s 3s str3ng 2x1mpl2....w4w >>> print s.translate(trantab, 'xm') th3s 3s str3ng 21pl2....w4w |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |